
/*
 * @(#)MaintMtmWorkflowProgressPriorityRuleAction.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.workflow.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.ErrorConstant;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.AbstractActionForm;
import com.dcivision.workflow.bean.MtmWorkflowStepWorkflowStep;
import com.dcivision.workflow.bean.WorkflowStep;
import com.dcivision.workflow.dao.MtmWorkflowStepWorkflowStepDAObject;
import com.dcivision.workflow.dao.WorkflowStepDAObject;

/**
 * MaintMtmWorkflowProgressPriorityRuleAction.java
 * @author      Tony Chen
 * @company     DCIVision Limited
 * @creation date   23/11/2004
 * @version     $Revision: 1.4 $
 */


public class MaintMtmWorkflowProgressPriorityRuleAction extends com.dcivision.framework.web.AbstractMaintAction {

  public static final String REVISION = "$Revision: 1.4 $";


  /** Creates a new instance of MaintMtmWorkflowProgressPriorityRuleAction */
  public MaintMtmWorkflowProgressPriorityRuleAction() {
    super();
  }

  /** getFunctionCode
   *
   * Abstract function which sub-class should implement to return the corresponding
   * function code.
   *
   * @return   The function code
   */
  public String getFunctionCode() {
    return SystemFunctionConstant.WORKFLOW_TASK;
  }

  /** getMajorDAOClassName
   *
   * Abstract function which sub-class should implement to return the corresponding
   * major DAO class name used in this class.
   *
   * @return   The DAO class name
   */
  public String getMajorDAOClassName() {
    return "com.dcivision.workflow.dao.MtmWorkflowProgressPriorityRuleDAObject";
  }

  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException {
    MaintMtmWorkflowStepPriorityRuleForm maintForm = (MaintMtmWorkflowStepPriorityRuleForm)form;

    String opMode = (maintForm != null) ? maintForm.getOpMode() : request.getParameter("opMode"); //maintForm.getOpMode();
    String navMode = (maintForm != null) ? maintForm.getNavMode() : request.getParameter("navMode"); //maintForm.getNavMode();

    String currentStepID = request.getParameter("currentStepID");
    if(!Utility.isEmpty(currentStepID)){
      maintForm.setCurrentStepID(currentStepID);
    }

    currentStepID = maintForm.getCurrentStepID();

    String wfRecordID = request.getParameter("workflowRecordID");


    WorkflowStepDAObject stepDAO = new WorkflowStepDAObject(this.getSessionContainer(request),this.getConnection(request));
    MtmWorkflowStepWorkflowStepDAObject stepStepDAO = new MtmWorkflowStepWorkflowStepDAObject(this.getSessionContainer(request),this.getConnection(request));

    List mtmStepList = new ArrayList();
    if (!Utility.isEmpty(currentStepID)){
        mtmStepList = stepStepDAO.getListByParentStepID(new Integer(currentStepID));
    } else {
      WorkflowStep wfStep = (WorkflowStep)stepDAO.getSubmitStepByWorkflowRecordID(new Integer(wfRecordID));
      mtmStepList = stepStepDAO.getListByParentStepID(wfStep.getID());
    }

    List childStepList = new ArrayList();

    for(int j=0; j<mtmStepList.size(); j++){
      MtmWorkflowStepWorkflowStep stepStep = (MtmWorkflowStepWorkflowStep)mtmStepList.get(j);
      WorkflowStep tmpWfStep = (WorkflowStep)stepDAO.getObjectByID(stepStep.getChildStepID());
      if(GlobalConstant.TRUE.equals(tmpWfStep.getAllowAssignPriority()) ){
        childStepList.add(tmpWfStep);
      }
    }

    request.setAttribute("workflowStepList",childStepList);


    if (GlobalConstant.OP_MODE_REFRESH.equals(opMode)) {
      // Set the function code for current action.
      ActionForward forward = this.retrieveFunctionCode(request, response, mapping);
      if (forward != null) {
        return forward;
      }
      refreshFormData(mapping, maintForm, request, response);
      return mapping.findForward(GlobalConstant.NAV_MODE_REFRESH);
    } else {
      return super.execute(mapping, form, request, response);
    }
  }

  public void refreshFormData(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws
    ApplicationException {
    List resultList = new ArrayList();

    try {
      SessionContainer sessionContainer = this.getSessionContainer(request);
      MaintMtmWorkflowStepPriorityRuleForm priorityRuleForm = (MaintMtmWorkflowStepPriorityRuleForm)form;
      WorkflowStepDAObject stepDAO = new WorkflowStepDAObject(this.getSessionContainer(request), this.getConnection(request));

      WorkflowStep step = (WorkflowStep)stepDAO.getObjectByID(new Integer(priorityRuleForm.getWorkflowStepID()));

      String priorityRuleListValue = "";
      String priorityRule = "";
      String[] tmpRuleArr = null;
      priorityRule = priorityRuleForm.getWorkflowStepID() + "," +
            priorityRuleForm.getEscalateAtAmt() + "," +
            priorityRuleForm.getEscalateAtField() + "," +
            priorityRuleForm.getEscalateTrigger() + "," +
            priorityRuleForm.getEscalateLevel() + "," +
            priorityRuleForm.getEscalateAckMethodByEmail() + "," +
            priorityRuleForm.getEscalateAckMethodBySystem() + "," +
            step.getStepName();

      if(!Utility.isEmpty(request.getParameter("priorityRuleListValue")) && !"null".equals(request.getParameter("priorityRuleListValue"))){
        priorityRuleListValue = request.getParameter("priorityRuleListValue");
      }

      if(!Utility.isEmpty(priorityRuleListValue)){
        tmpRuleArr = TextUtility.splitString(priorityRuleListValue,"|");
      }

      String[] deleteItemArr = request.getParameterValues("deleteItem");
      boolean checked = false;
      priorityRuleListValue = "";

      for(int j=0; !Utility.isEmpty(tmpRuleArr) &&  j<tmpRuleArr.length; j++){
        checked = false;
        for(int i=0; !Utility.isEmpty(deleteItemArr) && i<deleteItemArr.length; i++){
          if(j == Integer.parseInt(deleteItemArr[i])){
            checked = true;
            break;
          }
        }
        if(!checked){
          if(Utility.isEmpty(priorityRuleListValue)){
            priorityRuleListValue += tmpRuleArr[j];
          }else{
            priorityRuleListValue += "|" + tmpRuleArr[j];
          }
        }
      }

      if(Utility.isEmpty(priorityRuleListValue)){
        priorityRuleListValue = priorityRule;
      } else {
        priorityRuleListValue += "|" + priorityRule;
      }

      request.setAttribute("priorityRuleListValue", priorityRuleListValue);

    } catch (Exception e) {
      log.error(e, e);
      throw new ApplicationException(ErrorConstant.COMMON_FATAL_ERROR, e);
    } finally {
    }
  }


}
